package com.wawayaya.parentteacher.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;


import com.wawayaya.parentteacher.bean.Books;
import com.wawayaya.util.ImageUtils;
import com.wawayaya.util.TLog;

import java.util.ArrayList;
import java.util.List;

import wawayaya.kids_iread.R;

/**
 * Created by wang_qian on 2015/12/7.
 */
public class TpBookCoverAdapter extends RecyclerView.Adapter<TpBookCoverHolder>{
    private final String  TAG = getClass().getSimpleName();
    private Context context;
    private List<Books> books;
    private boolean   isZh;

    public TpBookCoverAdapter(Context context,boolean isZh, List<Books> books) {
        this.context = context;

        this.isZh = isZh;
        this.books = books;
    }


    @Override
    public int getItemCount() {
        int count = 0;
        if( books.size()>4){
            count = 4;
        }else{
            count = books.size();
        }
        return  count;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public TpBookCoverHolder onCreateViewHolder(ViewGroup vg, final int position) {
        View v = LayoutInflater.from(context).inflate(R.layout.tp_bookcover_layout, vg, false);
        return new TpBookCoverHolder(v);
    }

    private TpBookCoverHolder  holder;

    @Override
    public void onBindViewHolder(final TpBookCoverHolder holder, final int position) {
        final Books  book = books.get(position);
        this.holder = holder;
        holder.mTvName.setText(isZh?book.getNameZh():book.getNameEn());

        holder.mChkChoose.setChecked(book.getChoosed()?true:false);

        String  str = "http://wawaqinpic.b0.upaiyun.com/";  //http://10.135.195.180:8086/
        ImageUtils.getImageLoader().displayImage(str+ book.getThumb(), holder.mIvCover, ImageUtils.getCoverOptions());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TLog.log(TAG, "....on click...position:" + position);

            }
        });

        holder.mChkChoose.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                book.setChoosed(isChecked);
            }
        });
    }
}
